package com.sino.operation.config.swagger.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
@Component
public class ApplicationRunnerImpl implements ApplicationRunner {
    @Autowired
    WebApplicationContext applicationContext;
    @Override
    public void run(ApplicationArguments args) throws Exception {
        RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
        // 拿到Handler适配器中的全部方法
        Map<RequestMappingInfo, HandlerMethod> methodMap = mapping.getHandlerMethods();
        List<String> urlList = new ArrayList<>();
        for (RequestMappingInfo info : methodMap.keySet()){

            Set<String> urlSet = info.getPatternsCondition().getPatterns();
            // 获取全部请求方式
            /*Set<RequestMethod> Methods = info.getMethodsCondition().getMethods();
            System.out.println(Methods.toString());*/
            for (String url : urlSet){
                String[] strings = url.split("/");
                System.out.println(strings[strings.length-1]);
                // 加上自己的域名和端口号，就可以直接调用
                urlList.add("http://localhost:XXXX"+url);
            }
        }
       /* System.out.println(urlList.toString());*/
    }
}
